package com.zk.spring5.service;

import com.zk.spring5.dao.AccountDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ZK
 * @Description
 * @time 2021/5/8 10:35
 * @note
 * propagation属性默认为REQUIRED
 * isolation属性在MYSQL中默认为REPETABALE_READ，可以解决脏读和不可重复读，但仍会存在幻读
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.REPEATABLE_READ, timeout = 3600, readOnly = false,
        rollbackFor = {ClassCastException.class}, noRollbackFor = {NullPointerException.class})
public class AccountService {
    @Autowired
    private AccountDao accountDao;

    public void transfers(Integer payerId, Integer receiverId, double money) {
        accountDao.payMoney(payerId,money);
//        int a = 12 / 0; // 引入错误
        accountDao.receiveMoney(receiverId,money);
    }

}
